package com.yeswater.redis7.controller;

import com.yeswater.redis7.entity.Product;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @author： yeswater
 * @create： 2024/6/28
 */

@RestController
@Slf4j
@Api(tags = "聚划算商品列表接口")
public class JHSProductController {
	public static final String JHS_KEY = "jhs";
	public static final String JHS_KEY_A = "jhs:a";
	public static final String JHS_KEY_B = "jhs:b";
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	/**
	 * 分页查询：在高并发的情况下，只能走redis查询，走db的话必定会把db打垮
	 *
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "/product/find", method = RequestMethod.GET)
	@ApiOperation("按照分页和每页显示容量，点击查看")
	public List<Product> find(int page, int size) {
		List<Product> list = null;
		
		long start = (page - 1) * size;
		long end = start + size - 1;
		
		try {
			//采用redis list数据结构的lrange命令实现分页查询
			list = this.redisTemplate.opsForList().range(JHS_KEY, start, end);
			if (CollectionUtils.isEmpty(list)) {
				//TODO 走DB查询
			}
			log.info("查询结果：{}", list);
		} catch (Exception ex) {
			//这里的异常，一般是redis瘫痪 ，或 redis网络timeout
			log.error("exception:", ex);
			//TODO 走DB查询
		}
		
		return list;
	}
	
	@RequestMapping(value = "/product/findab", method = RequestMethod.GET)
	@ApiOperation("防止热点key突然失效，AB双缓存架构")
	public List<Product> findAB(int page, int size) {
		List<Product> list = null;
		long start = (page - 1) * size;
		long end = start + size - 1;
		try {
			//采用redis list数据结构的lrange命令实现分页查询
			list = this.redisTemplate.opsForList().range(JHS_KEY_A, start, end);
			if (CollectionUtils.isEmpty(list)) {
				log.info("=========A缓存已经失效了，记得人工修补，B缓存自动延续5天");
				//用户先查询缓存A(上面的代码)，如果缓存A查询不到（例如，更新缓存的时候删除了），再查询缓存B
				this.redisTemplate.opsForList().range(JHS_KEY_B, start, end);
				//TODO 走DB查询
			}
			log.info("查询结果：{}", list);
		} catch (Exception ex) {
			//这里的异常，一般是redis瘫痪 ，或 redis网络timeout
			log.error("exception:", ex);
			//TODO 走DB查询
		}
		return list;
	}
}